package com.lv.service.repo

import android.content.Context
import android.os.Parcelable
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Database
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.Update
import com.lv.service.room.helper.InspectionSectionConverter
import kotlinx.parcelize.Parcelize

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * App的公共业务基础数据类
 */

//3、database
@Database(
    entities = [UserInfo::class],
    version = 1,
    exportSchema = false,
)
abstract class UserDatabase : RoomDatabase() {

    abstract val userDao: UserDao

    companion object {

//        private val MIGRATION_1_2 = object : Migration(1, 2) {
//            override fun migrate(database: SupportSQLiteDatabase) {
//                database.execSQL("ALTER TABLE tb_o_info_user ADD COLUMN duty TEXT")
//            }
//        }

        private const val USER_DB_NAME = "syy_o_user_info_db"
//        private const val USER_DB_NAME = "syy_o_user_db"

        @Volatile
        private var instance: UserDatabase? = null

        @Synchronized
        fun getInstance(context: Context): UserDatabase {
            return instance ?: Room.databaseBuilder(
                context,
                UserDatabase::class.java,
                USER_DB_NAME
            )
//                .addMigrations(MIGRATION_1_2)
                .build().also { instance = it }
        }
    }
}

//1、entity的定义
@TypeConverters(InspectionSectionConverter::class)
@Entity(tableName = "tb_o_info_user")
@Parcelize
data class UserInfo(
    @PrimaryKey
    var _id: Int,//主键
    val id: String,
    val password: String?,
    var phone: String?,
    val created: Long,
    val lastlogin: Long,
    var image: String? = null,
    var name: String?,
    val token: String?,
    val refreshtoken: String?,
    val txaccid: String?,
    val role: String?,
    val txaccidpassword: String?,
) : Parcelable
////1、entity的定义
//
//@Entity(tableName = "tb_cniao_user")
//data class CniaoUserInfo(
//    @PrimaryKey
//    val id: Int,//主键
//    val course_permission: Boolean,
//    val token: String?,
//    @Embedded
//    val user: User?
//) {
//    @Keep
//    data class User(
//        @ColumnInfo(name = "cniao_user_id")
//        val id: Int,//用户id
//        val logo_url: String?,//用户头像
//        val reg_time: String?,//用户注册时间
//        val username: String?//用户名
//    )
//}

//2、dao层的定义
@Dao
interface UserDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertUser(user: UserInfo)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun updateUser(user: UserInfo)

    @Delete
    fun deleteUser(user: UserInfo)

    // = , in , like
    @Query("select * from tb_o_info_user where _id=:id")
    fun queryLiveUser(id: Int = 0): LiveData<UserInfo>

    @Query("select * from tb_o_info_user where _id=:id")
    fun queryUser(id: Int = 0): UserInfo?

    @Query("select * from tb_o_info_user where _id =:id")
    suspend fun getUser(id: Int = 0): UserInfo?
}



